package cn.shengongying.api.config;

import cn.shengongying.anon.User;
import cn.shengongying.bean.BaseUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * author: RXK
 * date: 2022/1/5 星期三
 * desc:
 */
@Configuration
public class WebMvcConfigure implements HandlerMethodArgumentResolver, WebMvcConfigurer {


    private final WebInterceptor webInterceptor;

    public WebMvcConfigure(WebInterceptor webInterceptor) {
        this.webInterceptor = webInterceptor;
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(User.class);
    }

    @Override
    public BaseUser resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return new BaseUser(1L,"12345678901");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(this);
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(webInterceptor);
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }
}
